<?php 
/*
Plugin Name: Image Banner Widget
Plugin URI: http://shailan.com/wordpress/plugins/image-banner-widget
Description: This simple widget is intended for simplest use of image banners. You can use this widget to put image banners on your site. You can find more widgets, plugins and themes at <a href="shailan.com">http://shailan.com</a>.
Version: 1.3.1
Author: Matt Say
Author URI: http://shailan.com
*/

define('SHAILAN_IBW_VERSION','1.3.1');

/**
 * Banner Widget Class
 */
 
if(!class_exists('shailan_BannerWidget')){
	class shailan_BannerWidget extends WP_Widget {
		/** constructor */
		function shailan_BannerWidget() {
			$widget_ops = array('classname' => 'shailan_banner_widget', 'description' => __( 'Image only banner' ) );
			$this->WP_Widget('banner', __('Image Banner Widget'), $widget_ops);
			$this->alt_option_name = 'widget_banner';	
			
			if(is_admin()){
				wp_enqueue_script('image-banner-scripts', WP_PLUGIN_URL . '/image-banner-widget/admin.js', 'jQuery', '', TRUE );
				wp_enqueue_style('image-banner-styles', WP_PLUGIN_URL . '/image-banner-widget/admin.css');
			}
			
			$this->help_url = "http://shailan.com/wordpress/plugins/image-banner-widget/help/";
			
			$this->widget_defaults = array(
				'url' => 'http://',
				'link'	=> 'http://',
				'alt' => '',
				'title' => '',
				'category' => 'shailan-show-all-categories',
				'home' => 'on',
				'autofit' => 'on',
				'target' => '_self'
			);
			
		}

		/** @see WP_Widget::widget */
		function widget($args, $instance) {	
			global $post;
			global $wpdb, $wp_locale, $wp_query;
		
			if(! isset($instance['home']) ){ $instance['home'] = 'off'; }
			if(! isset($instance['autofit']) ){ $instance['autofit'] = 'off'; }
		
			extract( $args );
			$widget_options = wp_parse_args( $instance, $this->widget_defaults );
			extract( $widget_options, EXTR_SKIP );
		
			$c1 = (is_home() && 'on' == $home); // is home and home is selected
			$c2 = ((is_category() || is_single() || is_page()) && $category == 'shailan-show-all-categories');
			$c3 = (is_home() && $category == 'shailan-home-only');
			$c4 = (is_single() && in_category( $category, $post->ID ));
			$c5 = (is_category($category));
			
			if( $c1 || $c2 || $c3 || $c4 || $c5 ){	
			
			echo $before_widget; ?>
			<!-- Image Banner Widget by shailan v<?php echo SHAILAN_IBW_VERSION; ?> on WP<?php bloginfo('version'); ?>-->
			<a href="<?php echo $link; ?>" target="<?php echo $target ?>" ><img src="<?php echo $url; ?>" alt="<?php echo $alt; ?>" title="<?php echo $title; ?>" class="banner-image" <?php if('on' == $autofit){ echo 'width="100%"'; }?> /></a>
			<!-- /Image Banner Widget -->
			<?php echo $after_widget; 
			
			}
		}

		/** @see WP_Widget::update */
		function update($new_instance, $old_instance) {	
			if(! isset($old_instance['home']) ){ $old_instance['home'] = 'off'; }
			if(! isset($old_instance['autofit']) ){ $old_instance['autofit'] = 'off'; }
			return $new_instance;
		}

		/** @see WP_Widget::form */
		function form($instance) {
			if(! isset($instance['home']) ){ $instance['home'] = 'off'; }
			if(! isset($instance['autofit']) ){ $instance['autofit'] = 'off'; }
			$widget_options = wp_parse_args( $instance, $this->widget_defaults );
			extract( $widget_options, EXTR_SKIP );
			
			//print_r($widget_options);
							
			if( $url != 'http://' ){
			?>
				<p><div class="ibw-thumb">
					<div class="ibw-overlay">
						<span>Preview</span>
					</div>
					
					<img src="<?php echo $url ?>" />
				</div></p>
				
			<?php } ?>
			
			<p><label for="<?php echo $this->get_field_id('url'); ?>"><?php _e('Image URL:', 'image-banner-widget'); ?>
				<?php $this->help_link('image-url'); ?> 
				<input class="widefat" id="<?php echo $this->get_field_id('url'); ?>" name="<?php echo $this->get_field_name('url'); ?>" type="text" value="<?php echo $url; ?>" /></label></p>
				
			<p><label for="<?php echo $this->get_field_id('link'); ?>"><?php _e('Link:', 'image-banner-widget'); ?> 
				<?php $this->help_link('link-url'); ?> 
				<input class="widefat" id="<?php echo $this->get_field_id('link'); ?>" name="<?php echo $this->get_field_name('link'); ?>" type="text" value="<?php echo $link; ?>" /></label></p>
			
			<div class="ibw-advanced-toggle"><span onclick="jQuery(this).next('.ibw-advanced-options').slideToggle(200);"><?php _e('Advanced Settings &darr;', 'image-banner-widget'); ?></span>
			<div class="ibw-advanced-options" style="display:none;" >
			
			<p><label for="<?php echo $this->get_field_id('alt'); ?>"><?php _e('Alt:', 'image-banner-widget'); ?> 
				<?php $this->help_link('alternate-text'); ?> 
				<input class="widefat" id="<?php echo $this->get_field_id('alt'); ?>" name="<?php echo $this->get_field_name('alt'); ?>" type="text" value="<?php echo $alt; ?>" /></label></p>
				
			<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'image-banner-widget'); ?>
				<?php $this->help_link('title'); ?> 
				<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
			
			<p><label for="<?php echo $this->get_field_id('target'); ?>"><?php _e('Target:'); ?> 
				<select name="<?php echo $this->get_field_name('target'); ?>" id="<?php echo $this->get_field_id('target'); ?>" > 	
					<option value="_self" <?php if('_self' == $target){ echo "selected=\"selected\""; } ?> >Current frame</option>		
					<option value="_blank" <?php if('_blank' == $target){ echo "selected=\"selected\""; } ?> >New page/tab</option>
					<option value="_top" <?php if('_top' == $target){ echo "selected=\"selected\""; } ?> >Top frame</option>
				</select></label> <?php $this->help_link('target'); ?> </p>
			
			<p><label for="<?php echo $this->get_field_id('category'); ?>"><?php _e('Display for:'); ?>
				<select name="<?php echo $this->get_field_name('category'); ?>" id="<?php echo $this->get_field_id('category'); ?>" > 	
					<option value="shailan-show-all-categories" <?php if('shailan-show-all-categories' == $category){ echo "selected=\"selected\""; } ?> >All categories</option>		
					<option value="shailan-home-only" <?php if('shailan-home-only' == $category){ echo "selected=\"selected\""; } ?> >Homepage only</option>		
					 <?php 
					  $categories = get_categories(''); 
					  foreach ($categories as $cat) {  
						$option = '<option value="'.$cat->category_nicename .'" '. ( $cat->category_nicename == $category ? ' selected="selected"' : '' ) .'>';
						$option .= $cat->cat_name;
						$option .= '</option>\n';
						echo $option;
					  }
					 ?>
					</select></label> <?php $this->help_link('display-for'); ?> </p>

			<p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('home'); ?>" name="<?php echo $this->get_field_name('home'); ?>"<?php checked( $home, 'on' ); ?> />
			<label for="<?php echo $this->get_field_id('home'); ?>"><?php _e( 'Display on homepage'); ?> <?php $this->help_link('display'); ?></label></p>

			<p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('autofit'); ?>" name="<?php echo $this->get_field_name('autofit'); ?>"<?php checked( $autofit, 'on' ); ?> />
			<label for="<?php echo $this->get_field_id('autofit'); ?>"><?php _e( 'Auto-fit to container width' , 'image-banner-widget'); ?> <?php $this->help_link('autofit'); ?></label></p>

			</div>
			</div>
			
		<div class="widget-control-actions">
			<p><small>Powered by <a href="http://shailan.com/wordpress/plugins/image-banner-widget" title="Wordpress Tips and tricks, Freelancing, Web Design">Shailan.com</a> | <a href="http://shailan.com/wordpress/" title="Get more wordpress widgets and themes" target="_blank" >Get more..</a></small></p>
		</div>
		
				<div class="clear"></div>
				
			<?php 
			
		}
		
		function help_link($key, $text = '(?)'){
			echo '<a href="'.$this->help_url.'#' . $key. '" target="_blank" class="help-link">' . $text . '</a>';
 		}
		
	} // class shailan_BannerWidget
}
// register widget
add_action('widgets_init', create_function('', 'return register_widget("shailan_BannerWidget");'));